package com.wwwarehouse.contract.contract;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.contract.adapter.SettingRulesGoodsAdapter;
import com.wwwarehouse.contract.bean.SettingRuleBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.RefreshEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/10/19.
 *         <p/>
 *         商品设置规则
 */

public class ReleaseSettingRulesGoodsListFragment extends TheParentFragment {

    private RecyclerView mRvContent;
    private String pbUkid;
    private ArrayList<SettingRuleBean.DataBean.ListBean> tagBeanList = new ArrayList<>();
    SettingRulesGoodsAdapter mAdapter;
    private int num;
    private int pos;
    private String sort;

    @Override
    protected int getContentId() {
        return R.layout.release_object_set_rule_list;
    }

    @Override
    protected void init(View view) {
        mRvContent = findView(view, R.id.rv_content);
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void loadDatas() {
        Bundle b = getArguments();
        if (b == null)
            return;
        pbUkid = b.getString("pbUkid");
        pos = b.getInt("pos");
        num = b.getInt("num");
        sort = b.getString("sort");
        EventBus.getDefault().register(this);

        Map<String, Object> map = new HashMap<>();
        map.put("page", pos); //
        map.put("pbUkid", pbUkid); //  发布ukid
        map.put("size", num); //
        map.put("sort", sort); //     排序(DESC,ASC)
        httpPost(ContractConstant.GETPBRSITEMTEMPSBYPBUKID, map, 0, true, "");


    }

    public void onEventMainThread(Object event) {
        if (event instanceof RefreshEvent) {
            if (pos == ((RefreshEvent) event).getPage()) {
                tagBeanList.get(((RefreshEvent) event).getPosition()).setHasSetRule(true);
                tagBeanList.get(((RefreshEvent) event).getPosition()).setItemTempUkid(((RefreshEvent) event).getBean().getItemTempUkid());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setPriceRuleType(((RefreshEvent) event).getBean().getPriceRuleType());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setPriceRuleValue(((RefreshEvent) event).getBean().getPriceRuleValue());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setQtyRuleType(((RefreshEvent) event).getBean().getQtyRuleType());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setQtyRuleValue(((RefreshEvent) event).getBean().getQtyRuleValue());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setPrice(((RefreshEvent) event).getBean().getPrice());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setQty(((RefreshEvent) event).getBean().getQty());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setUnit(((RefreshEvent) event).getBean().getUnit());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setDefinedUkid(((RefreshEvent) event).getBean().getDefinedUkid());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setPbUkid(((RefreshEvent) event).getBean().getPbUkid());
                mAdapter.notifyItemChanged(((RefreshEvent) event).getPosition());
            }
        }
    }


    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (what == 0) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                SettingRuleBean.DataBean dataBean = JSON.parseObject(commonClass.getData().toString(), SettingRuleBean.DataBean.class);
                if (dataBean.getList() != null) {
                    tagBeanList.addAll(dataBean.getList());
                }
                mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 1) {
                    @Override
                    public boolean canScrollHorizontally() {
                        return false;
                    }

                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }

                });
                final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
                observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        observer.removeGlobalOnLayoutListener(this);
                        if (mAdapter == null) {
                            mAdapter = new SettingRulesGoodsAdapter(mActivity, tagBeanList, mRvContent.getHeight(), mRvContent.getWidth(), num, pos, pbUkid);
                        }
                        mRvContent.setAdapter(mAdapter);
                    }
                });

            } else {
                toast(commonClass.getMsg());
            }
        }
    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (mAdapter != null) {
            mAdapter.dissPop();
        }
    }
}
